using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using VSSDKAssist.Assets.Enums;
using System.Globalization;

namespace VSSDKAssist.Recipes.Common.Helpers
{
	/// <summary>
	/// Visual Studio Package helper
	/// </summary>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased")]
	public static class VSIPPackageHelper
	{
		#region Constructors
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
		static VSIPPackageHelper()
		{
			AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolve);
		}
		#endregion

		#region Public Implementation
		//public static void RegisterVSPackage(string assemblyPath, VsHive hive)
		//{
		//    TODO: Fix locking assemblies issue
		//    if(hive == VsHive.Standard)
		//    {
		//        RegPkg.Register(assemblyPath, Constants.VsHiveStandard, RegistrationMethod.Default);
		//    }
		//    else if(hive == VsHive.Experimental)
		//    {
		//        RegPkg.Register(assemblyPath, Constants.VsHiveExperimental, RegistrationMethod.Default);
		//    }
		//}

		//public static void UnRegisterVSPackage(string assemblyPath, VsHive hive)
		//{
		//    TODO: Fix locking assemblies issue
		//    if(hive == VsHive.Standard)
		//    {
		//        RegPkg.Unregister(assemblyPath, Constants.VsHiveStandard, RegistrationMethod.Default);
		//    }
		//    else if(hive == VsHive.Experimental)
		//    {
		//        RegPkg.Unregister(assemblyPath, Constants.VsHiveExperimental, RegistrationMethod.Default);
		//    }
		//} 
		#endregion

		#region Private Implementation
		private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
		{
			AssemblyName name = new AssemblyName(args.Name);
			if(string.Compare(name.Name, "RegPkg", true, CultureInfo.CurrentCulture) == 0)
			{
				string vsSdkToolsPath = Path.Combine(CommonHelper.GetVSSdkInstallPath(), Constants.VSSdkToolRelPath);
				string assemblyPath = Path.Combine(vsSdkToolsPath, Constants.RegPkgTool);

				return Assembly.LoadFrom(assemblyPath);
			}

			return null;
		}
		#endregion
	}
}